package viper.receive;

import java.lang.System;
import javax.media.*;
import viper.main.StateManager;

public class CallReceiver implements Runnable {
	//start player, start sliding window array that dediversifies and put
	//audio packets in player buffer
	
	public static int WINDOW_INDEX;
	public static final int WINDOW_SIZE = 20; //Tweak
	public static final int RCV_BUFFER_SZ = 3000; //Tweak
	public static final long TIMEOUT = 10; //Tweak; in miliseconds
	public static byte[][][] RCV_BUFFER;
	public static LiveStream RXLS;
	public static final int MAX_PKT_SZ = 9000;
	
	public void run() {
		//start player, start sliding window array	
		try{
			// Construct the custom PushBufferStream
			RXLS = new LiveStream();
			// Initialize receive stream with DataSource
			CustomDataSource rxcds = new CustomDataSource(RXLS);
			rxcds.connect(); 

			// Construct and start the audio player
			Player player = Manager.createRealizedPlayer(rxcds);
			player.start();
			System.out.println("Starting player...");

		} catch (Throwable t) {
			t.printStackTrace();
		}
		
		//Manage sliding window by calling a new SlidingWinThread
		while(StateManager.INSTANCE.getState()==StateManager.State.IN_CALL) {
			StateManager.INSTANCE.getPool().execute(new SlidingWinThread());
		}
		
		System.out.println("run CallReceiver");
	}
}
